$(function () {
    let globalStr = ''//  选中的股票代码
    $(document).keydown(function (event) {
        if ((event.keyCode >= 65 && event.keyCode <= 90) || (event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105)) {
            $("#search").focus();
        }
    });
    $(document).click(function (event) {
        $('#search_panel').css('display', 'none');
    })
    $('#search-wrapper').click(function (e) {
        e.stopPropagation();
    })
    /*
    * 获取搜索数据
    * */
    $("#search").bind('input', function () {
        // $("input").css("background-color","#D6D6FF");
        let value = $(this).val();
        $.ajax({
            type: "GET",
            url: "stock/search",
            dataType: "json",
            data: {word: value},
            success: function (data) {
                $('#search_panel').css('display', 'block');
                let dataArray = data.data;
                let searchHtml = '';
                if (dataArray) {
                    for (let item of dataArray) {
                        searchHtml += '<p><span>' + item.obj + '</span><span>' + item.zhongWenJianCheng + '</span><a class="no-add" code="' + item.obj + '"href="javascript:;">' + '添加</a></p>'
                    }
                } else {
                    $('#no_search_data').css('display', 'block');

                }
                $('#search_panel').html(searchHtml);
            }
        });
    });
    $('#search').bind('keyup', function (event) {
        if (event.keyCode == "13") {
            //回车执行查询
            $(this).blur();
        }
    });
    $(document).on("click", "#search_panel p a", function (e) {
        e.stopPropagation();
        let code = $(this).attr('code');
        if ($(this).hasClass('no-add') && !isRepeat(code)) {
            $(this).removeClass('no-add');
            globalStr += code + ',';
            $('#checked_code').text(globalStr);
        } else {
            alert('已添加！')
        }
    });
    /*
    * 判断是否有重复数据
    * */
    function isRepeat(code){
        let allArray = $('#checked_code').text();
        if(allArray.includes(code)){
            return true
        }else{
            return false;
        }
    }
    /*
    * 获取指标下拉按钮列表
    * */
    $.ajax({
        type: "GET",
        url: "stock/strategy",
        dataType: "json",
        success: function (data) {
            let dataArray = data.data;
            let htmlJson = '';
            for (let item of dataArray) {
                htmlJson += '<option p-tp=' + item.id + '>' + item.strategyName + '</option>'
            }
            $('#choiceSelect').html(htmlJson);
        }
    });

    /*
    * 点击按钮获取结果
    * */
    function requestList(str, callback) {
        $.ajax({
            type: "GET",
            url: "stock/data",
            dataType: "json",
            data: {ids: str},
            success: function (data) {
                callback(data.data);
            }
        });
    }

    $('#submit_btn').click(function () {
        $('.current-label').html('功能&gt;'+$('#choiceSelect').val())
        $('.menu_list').slideUp('fast');
        $('.content-panel .table-wrapper').slideDown('fast')
        let htmlStr = '';
        requestList($('#checked_code').text(), function (data) {
            if(!data){
                return;
            }
            data.forEach(function (item, index) {
                if (item.zhongWenJianCheng.indexOf('退市') < 0) {
                    htmlStr += `<tr>
            <td class="col1">${index + 1}</td>
            <td class="col2">${item.obj}</td>
            <td class="col2">${item.zhongWenJianCheng}</td>
            ${
                (function(){
                    if(item.zhangFu > 0){
                        return `<td class="col4 rColor">${item.zhangFu ? item.zhangFu : 0}%</td>
                        <td class="col5 rColor">${item.weiTuoMaiChuJia1 ? item.weiTuoMaiChuJia1.toFixed(2) : '-'}</td>
                        <!-- 现价 -->
                        
                        <td class="col6 rColor">${item.zuiXinJia ? item.zuiXinJia.toFixed(2) : 0}</td>`;
                    }else if(item.zhangFu < 0){
                        return `<td class="col4 gColor">${item.zhangFu ? item.zhangFu : 0}%</td>
                        <td class="col5 gColor">${item.weiTuoMaiChuJia1 ? item.weiTuoMaiChuJia1.toFixed(2) : '-'}</td>
                        <!-- 现价 -->
                        <td class="col6 gColor">${item.zuiXinJia ? item.zuiXinJia.toFixed(2) : 0}</td>`;
                    }else{
                        return `<td class="col4 wColor">${item.zhangFu ? item.zhangFu : 0}%</td>
                        <td class="col5 wColor">${item.weiTuoMaiChuJia1 ? item.weiTuoMaiChuJia1.toFixed(2) : '-'}</td>
                        <!-- 现价 -->
                        <td class="col6 wColor">${item.zuiXinJia ? item.zuiXinJia.toFixed(2) : 0}</td>`;
                    }
                    // ``
                })()
            }
            <!-- 成交量  -->
            <td class="col7">${item.chengJiaoLiang ? (item.chengJiaoLiang / 100 / 10000).toFixed(2) + '万' : '-'}</td>
            <!-- 总金额 -->
            ${
                        (function(){
                            if(item.chengJiaoE){
                                if(item.chengJiaoE >= 100000000){
                                    return `<td class="col7">${(item.chengJiaoE / 100000000).toFixed(1) + '亿'}</td>`;
                                }else {
                                    return `<td class="col7">${(item.chengJiaoE / 10000).toFixed(1) + '万'}</td>`;
                                }
                            }else{
                                return `<td class="col7">-</td>`;
                            }
                        })()
                    }
            
            <!-- 所属行业 -->
            <td class="col7">${item.hangYe}</td>
            <!-- 换手率 -->
            <td class="col10">${item.huanShou ? item.huanShou.toFixed(2) : 0}%</td>
            <!-- 市盈率 -->
            ${
                        (function(){
                            if(item.shiYingLv){
                                if(item.shiYingLv > 0){
                                    return `<td class="col11 rColor">${item.shiYingLv.toFixed(2)}</td>`
                                }else if(item.shiYingLv < 0){
                                    return `<td class="col11 gColor">${item.shiYingLv.toFixed(2)}</td>`
                                }else {
                                    return `<td class="col11 wColor">${item.shiYingLv.toFixed(2)}</td>`
                                }
                            }else{
                                return `<td class="col11 wColor">0</td>`
                            }
                        })()
                    }
            
            <!-- 市净率 -->
            ${
                        (function(){
                            if(item.shiJingLv){
                                if(item.shiJingLv > 0){
                                    return `<td class="col11 rColor">${Number(item.shiJingLv).toFixed(2)}%</td>`
                                }else if(item.shiJingLv < 0){
                                    return `<td class="col11 gColor">${Number(item.shiJingLv).toFixed(2)}%</td>`
                                }else {
                                    return `<td class="col11 wColor">${Number(item.shiJingLv).toFixed(2)}%</td>`
                                }
                            }else{
                                return `<td class="col11 wColor">0</td>`

                            }
                        })()
                    }
      
            <!-- 市销率 -->
            ${
                        (function(){
                            if(item.shiXiaoLv){
                                if(item.shiXiaoLv > 0){
                                    return `<td class="col11 rColor">${Number(item.shiXiaoLv).toFixed(2)}%</td>`
                                }else if(item.shiXiaoLv < 0){
                                    return `<td class="col11 gColor">${Number(item.shiXiaoLv).toFixed(2)}%</td>`
                                }else {
                                    return `<td class="col11 wColor">${Number(item.shiXiaoLv).toFixed(2)}%</td>`
                                }
                            }else {
                                return `<td class="col11 wColor">0</td>`
                            }
                            
                        })()
                    }
        
            <!-- 每股经营现金 -->
            ${
                        (function(){
                            if(item.meiGuJingYingXianJin){
                                if(item.meiGuJingYingXianJin > 0){
                                    return `<td class="col11 rColor">${item.meiGuJingYingXianJin ? Number(item.meiGuJingYingXianJin).toFixed(2) : 0}</td>`
                                }else if(item.meiGuJingYingXianJin < 0){
                                    return `<td class="col11 gColor">${item.meiGuJingYingXianJin ? Number(item.meiGuJingYingXianJin).toFixed(2) : 0}</td>`
                                }else {
                                    return `<td class="col11 wColor">${item.meiGuJingYingXianJin ? Number(item.meiGuJingYingXianJin).toFixed(2) : 0}</td>`
                                }
                            }else{
                                return `<td class="col11 wColor">0</td>`
                            }
                            
                        })()
                    }
            <!-- 销售毛利率 -->
            ${
                        (function(){
                            if(item.xiaoShouMaoLiLv){
                                if(item.xiaoShouMaoLiLv > 0){
                                    return `<td class="col11 rColor">${Number(item.xiaoShouMaoLiLv).toFixed(2)}%</td>`
                                }else if(item.xiaoShouMaoLiLv < 0){
                                    return `<td class="col11 gColor">${Number(item.xiaoShouMaoLiLv).toFixed(2)}%</td>`
                                }else {
                                    return `<td class="col11 wColor">${Number(item.xiaoShouMaoLiLv).toFixed(2)}%</td>`
                                }
                            }else{
                                return `<td class="col11 wColor">0</td>`
                            }
                            
                        })()
                    }
            <!-- 资产负债率 -->
            ${
                        (function(){
                            if(item.ziChanFuZhaiLv){
                                if(item.ziChanFuZhaiLv > 0){
                                    return `<td class="col11 rColor">${Number(item.ziChanFuZhaiLv).toFixed(2)}%</td>`
                                }else if(item.ziChanFuZhaiLv < 0){
                                    return `<td class="col11 gColor">${Number(item.ziChanFuZhaiLv).toFixed(2)}%</td>`
                                }else {
                                    return `<td class="col11 wColor">${Number(item.ziChanFuZhaiLv).toFixed(2)}%</td>`
                                }
                            }else{
                                return `<td class="col11 wColor">0</td>`
                            }
                            
                        })()
                    }
            <!-- 净资产收益率 -->
            ${
                        (function(){
                            if(item.jingZiChanShouYiLv){
                                if(item.jingZiChanShouYiLv > 0){
                                    return `<td class="col11 rColor">${Number(item.jingZiChanShouYiLv).toFixed(2)}%</td>`
                                }else if(item.jingZiChanShouYiLv < 0){
                                    return `<td class="col11 gColor">${Number(item.jingZiChanShouYiLv).toFixed(2)}%</td>`
                                }else {
                                    return `<td class="col11 wColor">${Number(item.jingZiChanShouYiLv).toFixed(2)}%</td>`
                                }
                            }else{
                                return `<td class="col11 wColor">0</td>`
                            }
                        })()
                    }
            <!-- 量比 -->
            <td class="col10">${item.liangBi}</td>
            <!-- 振幅 -->
            <td class="col10">${item.zhenFu}</td>
            <!-- 上市日期 -->
            <td class="col7">${item.shangShiRiQi ? (item.shangShiRiQi.substring(0,4)+'-'+item.shangShiRiQi.substring(4,6)+'-'+item.shangShiRiQi.substring(6)) : '-'}</td>
        </tr>`
                }
            })
            $('.xgcl .content-panel tbody').html(htmlStr)
        })
    })
})